import 'package:flutter/material.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:zootix/models/zt_im_conversation.model.dart';
import 'package:zootix/screens/follow_conversation/zt_conversation_item.dart';
import 'package:zootix/screens/follow_conversation/zt_conversations.controller.dart';
import 'package:zootix/store/zt_im_chat.store.dart';
import 'package:zootix/util/zt_screen_adaper_util.dart';
import 'package:zootix/widgets/zt_empty.dart';
import 'package:zootix/widgets/zt_fade_in_cache_image.dart';
import 'package:zootix/constants/zt_router.dart';

class ZtConversationList extends StatelessWidget {
  final _controller =
      Get.put<ZtConversationController>(ZtConversationController());
  ZtConversationList({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ZtConversationController>(
        init: _controller,
        builder: (_) {
          return !Get.isRegistered<ZtImChartStore>()
              ? ZtEmpty(text: "Waiting IM init.")
              : _buildList();
        });
  }

  Widget _buildList() {
    return Obx(() => SmartRefresher(
        enablePullDown: true,
        enablePullUp: true,
        controller: _controller.refreshController,
        onRefresh: _controller.onRefresh,
        onLoading: _controller.onLoading,
        child: ZtImChartStore.to.conversationList.isEmpty &&
                [RefreshStatus.completed]
                    .contains(_controller.refreshController.headerStatus)
            ? ZtEmpty(text: "No messages.")
            : ListView.builder(
                itemCount: ZtImChartStore.to.conversationList.length,
                itemBuilder: (_, index) => _buildMessageItem(index))));
  }

  Widget _buildMessageItem(int index) {
    return ZtConversationItem(
      conversationModel: ZtImChartStore.to.conversationList[index],
      index: index,
    );
  }

  // String _buildTime(String time) {
  //   // Duration duration = DateTime.parse(time).difference(DateTime.now());

  //   // Duration duration1 =
  //   //     DateTime.parse(time).difference(DateTime(DateTime.now().year, 1, 1));
  //   List<String> format = [];
  //   if (duration.inDays < 1) {
  //     format = ['hh', ':', 'nn'];
  //   } else if (duration.inDays > 1 && duration.inDays)
  //     return formatDate(
  //         DateTime.parse(time), ['mm', '-', 'dd', ' ', 'hh', ':', 'nn']);
  // }
}
